package ${gen.packageName}.${gen.subPackageName}.dao.base;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import ${gen.packageName}.${gen.subPackageName}.model.${entity.MODEL_NAME};
import com.msteam.app.sys.model.Metadata;
import com.msteam.app.sys.model.query.MetaDataQuery;
import com.msteam.kernel.orm.dao.BaseDao;

/**
 * ${entity.DISPLAY_NAME} 查询基类
 * @author Jadranka
 *
 */
public class Base${entity.MODEL_NAME}Dao  extends BaseDao<${entity.MODEL_NAME}> {
	protected static final ${entity.MODEL_NAME} me = new ${entity.MODEL_NAME}();

	/**
	 * 构造方法
	 */
	public Base${entity.MODEL_NAME}Dao() {
		super(${entity.MODEL_NAME}.class);
	}
	
	public ${entity.MODEL_NAME} findFirstFromMetadata(String key, Object value) {
		//return me.findFirstFromMetadata(key, value);
		Metadata md = MetaDataQuery.me().findFirstByTypeAndValue(${entity.MODEL_NAME}.METADATA_TYPE, key, value);
		if (md != null) {
			BigInteger id = md.getObjectId();
			return _findByKey(id.longValue());
		}
		return null;
	}
	// 按 编号 查询单条
	public ${entity.MODEL_NAME} _findByKey(final Long id) {
		return me.doFindFirst("ID = ?", id);
	}
	/**
	 * 普通分页
	 * @param pageNumber 页号
	 * @param pageSize 每页记录数
	 * @param orderby 排序
	 * @return
	 */
	public Page<${entity.MODEL_NAME}> paginate(int pageNumber, int pageSize , String orderby) {
		return paginate(pageNumber, pageSize, "", orderby);
	}

	/**
	 * 条件查询分页
	 * @param pageNumber 页号
	 * @param pageSize 每页记录数
	 * @param where 条件 eg. NAME=?
	 * @param orderby 排序
	 * @param paras 参数
	 * @return
	 */
	public Page<${entity.MODEL_NAME}> paginate(int pageNumber, int pageSize , String where, String orderby, Object... paras) {
		String select = "select * ";
		StringBuilder fromBuilder = new StringBuilder(" from ${entity.TABLE_NAME} t ");
		if(!StrKit.isBlank(where)){
			fromBuilder.append(" where "+ where + " ");
		}
		if(!StrKit.isBlank(orderby)){
			buildOrderBy(orderby, fromBuilder);
		}
		return me.paginate(pageNumber, pageSize, select, fromBuilder.toString(), paras);
	}

	<#list entity.fields as field>
	<#if field.extra.listPageExtSearch==true>
	/**
	 * 按 ${field.DISPLAY_NAME} 查询分页
	 * @param ${field.ATTR_NAME?lower_case} ${field.DISPLAY_NAME}
	 * @param pageNumber 页号
	 * @param pageSize 每页记录数
	 * @param orderby 排序字段
	 * @return
	 */
	public Page<${entity.MODEL_NAME}> paginateBy${field.ATTR_NAME?cap_first}(${field.JAVA_TYPE} ${field.ATTR_NAME?lower_case}, int pageNumber, int pageSize, String orderby) {
		return paginate(pageNumber, pageSize, "${field.NAME}=?", orderby, ${field.ATTR_NAME?lower_case});
	}

	</#if>
	</#list>

	public long findCount() {
		return me.doFindCount();
	}


	<#list entity.fields as field>
	<#if field.extra.listPageExtSearch==true>
	// 按 ${field.DISPLAY_NAME} 查询单条
	public ${entity.MODEL_NAME} findBy${field.ATTR_NAME?cap_first}(final ${field.JAVA_TYPE} ${field.ATTR_NAME?uncap_first}) {
		return me.doFindFirst("${field.NAME} = ?", ${field.ATTR_NAME?uncap_first});
		/*return DAO.getCache(${field.ATTR_NAME?uncap_first}, new IDataLoader() {
			@Override
			public Object load() {
				return me.doFindFirst("${field.NAME} = ?", ${field.ATTR_NAME?uncap_first});
			}
		});*/
	}
	</#if>
	</#list>

	/**
	 * 排序条件
	 * @param orderBy
	 * @param fromBuilder
	 */
	protected static void buildOrderBy(String orderBy, StringBuilder fromBuilder) {
		
		<#list entity.fields as field>
		<#if field.extra.listPageExtSearch==true>
		// 按 ${field.DISPLAY_NAME} 排序
		if ("${field.NAME}".equals(orderBy)) {
			fromBuilder.append(" ORDER BY t.${field.NAME} DESC");
			return;
		}
		</#if>
		</#list>
	}

}
